﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Windows;

namespace ImageRecognition2.Forms
{
    /// <summary>
    /// 
    /// </summary>
    public partial class FormChildList : Form
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="FormChildList"/> class.
        /// </summary>
        public FormChildList()
        {
            InitializeComponent();
            Initialize();
        }

        private void Initialize()
        {
            cbOK.Parent = this.pictureBox1;
            this.pictureBox1.BackgroundImageLayout = ImageLayout.Zoom;
            this.cbOK.Click += new EventHandler(cbOK_Click);
            if (this.listBox1.Items.Count > 0)
            {
                this.listBox1.SelectedIndex = 0;
                SelectPicture();
            }
        }

        /// <summary>
        /// Reinitializes the list box.
        /// </summary>
        protected void ReinitializeListBox()
        {
            listBox1.Items.Clear();
            foreach (Form form in Application.OpenForms)
            {
                if (form.GetType() == typeof(FormChild))
                {
                    this.listBox1.Items.Add(new FormItem((FormChild)form));
                }
            }
        }

        protected override void OnActivated(EventArgs e)
        {
            base.OnActivated(e);
            ReinitializeListBox();
            SelectPicture();
        }

        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            SelectPicture();
        }

        private void listBox1_Click(object sender, EventArgs e)
        {
            SelectPicture();
        }

        /// <summary>
        /// Selects the picture.
        /// </summary>
        private void SelectPicture()
        {
            if (this.listBox1.SelectedItem == null) return;
            this.pictureBox1.BackgroundImage = ((FormItem)this.listBox1.SelectedItem).Form.pictureBox1.BackgroundImage;
        }

        /// <summary>
        /// Gets the form item.
        /// </summary>
        /// <value>The form item.</value>
        public FormItem FormItem
        { 
            get { return (FormItem)this.listBox1.SelectedItem; } 
        }

        private void cbOK_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.OK;
            foreach (FormItem item in this.listBox1.Items)
            {
                if (item.Form.pictureBox1.BackColor != Color.Black)
                {
                    item.Form.pictureBox1.BackColor = Color.Black;
                }
            }
            this.Close();
        }

        private void listBox1_SelectedIndexChanged_1(object sender, EventArgs e)
        {
            FormItem item = this.FormItem;
            item.Form.pictureBox1.BackColor = Color.White;
            item.Form.Invalidate();
            item.Form.pictureBox1.Invalidate();
            Win32Helpers.FlashWindow(item.Form.Handle, false);
        }
    }

    public class FormItem : object
    {
        private FormChild formChild = null;
        public FormItem(FormChild _formChild)
        {
            formChild = _formChild;
        }

        public override string ToString()
        {
            return formChild.ToString();
        }

        public FormChild Form
        {
            get { return formChild; }
            set { formChild = value; }
        }
    }
}
